#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include "gd32f4xx.h"
#include "systick.h"
#include "gd32f470i_eval.h"

#include "bs_app.h"
#include "lcdfont.h"
#include "lcd.h"
#include "lcd_init.h"
#include "u8g2.h"
#include "st7789v2.h"
#include "menu.h"
#include "dev_key.h"

#define LOG_TAG        "[lcd] "
#include "log.h"

// 按键消息
extern uint32_t key_msg;

u8g2_t __u8g2;
u8g2_t *u8g2 = &__u8g2;


uchar func_index = 2;
uchar last_index = 99; 

void bs_dev_lcd_init(void)
{
#if LCD_EN
    u8g2_Setup_demo(u8g2, U8G2_R0, u8g2_Setup_demo_byte_cb, u8g2_Setup_demo_gpio_and_delay_cb);
    u8g2_InitDisplay(u8g2);
    u8g2_ClearBuffer(u8g2);
    u8g2_SetPowerSave(u8g2, 1);
    u8g2_SetFont(u8g2, u8g2_font_wqy12_t_gb2312b);


    /******************************屏幕按键初始化**********************************************/
    // key_Init();
    LCD_Init();
    LCD_DisplayOn();
    // LCD_Fill(0, 0, 320, 240, 0xFFFF);
    u8g2_ClearBuffer(u8g2);
    fun_main_loading();
    u8g2_SendBuffer(u8g2);
    u8g2_SetPowerSave(u8g2,0);
#endif
}

#if LCD_EN


void bs_key_long_press_cb(bs_key_t *key)
{
    void (*current_operation_index)();
    key_msg = MSG_KLONGPRESS;
        switch(key->id) {

        case 2:
            LOGI("long press... \r\n");
            func_index = table[func_index].back;
        break;

        default:
        break;
    }

    // if (func_index != last_index)
    {
        current_operation_index = table[func_index].current_operation;
        
        u8g2_ClearBuffer(u8g2);
        (*current_operation_index)();
        u8g2_SendBuffer(u8g2);
        u8g2_SetPowerSave(u8g2,0);

        last_index = func_index;
    }
    return;
}



/*!
    \brief      lcd模块主程序
    \param[in]  none
    \param[out] none
    \retval
*/
void bs_key_onclick_cb(bs_key_t *key)
{
    void (*current_operation_index)();

    key_msg = (uint8_t)key->id;

#if 1
    switch(key_msg) {
        case 0:
            func_index = table[func_index].up;
        break;

        case 1:
            func_index = table[func_index].down;
        break;


        case 2:
            func_index = table[func_index].enter;
        break;
    }

    // if (func_index != last_index)
    {
        current_operation_index = table[func_index].current_operation;
        
        u8g2_ClearBuffer(u8g2);
        (*current_operation_index)();
        u8g2_SendBuffer(u8g2);
        u8g2_SetPowerSave(u8g2,0);

        last_index = func_index;
    }

#else
/***********************test*************************************************/
    int i=0;
    char tmp_str[] = {"Hello World! 100"};

    while(0){
        // LCD_Color_Fill_DMA(0,0,239,239,(uint16_t *)kunkun);
    }


    while(1){
    sprintf(tmp_str,"Hello World! %3d", i);
    i++;
    u8g2_DrawStr(u8g2,10, 10, tmp_str);
    u8g2_DrawStr(u8g2,10, 40, tmp_str);
    u8g2_DrawStr(u8g2,10, 70, tmp_str);
    u8g2_DrawStr(u8g2,10, 100, tmp_str);
    u8g2_DrawButtonUTF8(u8g2,200, 20, U8G2_BTN_INV, 0,  2,  2, tmp_str );
    u8g2_DrawLine(u8g2,0, 0, 319, 239);
    u8g2_DrawLine(u8g2,319, 0, 0, 239);
    u8g2_DrawButtonUTF8(u8g2,5, 130, U8G2_BTN_INV, u8g2_GetDisplayWidth(u8g2)-5*2,  5,  2, tmp_str );
    u8g2_SendBuffer(u8g2);
    u8g2_SetPowerSave(u8g2, 0);
    u8g2_ClearBuffer(u8g2);	
}
/***************************test_end*******************************/
	while(1){

	}
#endif
	return;
}
#endif
